package com.cate.controller.user;

import com.cate.pojo.dto.ShoppingCartDTO;
import com.cate.pojo.entity.ShoppingCart;
import com.cate.result.RestResponse;
import com.cate.service.ShoppingCartService;
import com.cate.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @CreateTime: 2023/8/1
 * @Author: <==lhd==>
 * @Description:
 */

@RestController("UserShoppingCartController")
@Slf4j
@RequestMapping("/user/shoppingCart")
@Api(tags = "用户端购物车接口")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public RestResponse cleanCart(){
        shoppingCartService.cleanShoppingCart();
        return ResultUtil.success("");
    }

    @GetMapping("/list")
    @ApiOperation("查看购物车")
    public RestResponse<List<ShoppingCart>> getCartlist(){
        List<ShoppingCart> list = shoppingCartService.showShoppingCart();
        return ResultUtil.success(list);
    }


    @PostMapping("/sub")
    @ApiOperation("删除购物车中一个商品")
    public RestResponse<Object> subCart(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("删除购物车中一个商品，商品：{}", shoppingCartDTO);
        shoppingCartService.subShoppingCart(shoppingCartDTO);
        return ResultUtil.success("");
    }

    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public RestResponse<Object> addCart(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加购物车，商品信息为：{}",shoppingCartDTO);
        shoppingCartService.addShoppingCart(shoppingCartDTO);
        return ResultUtil.success("");
    }

}
